Skip to content

fix(agent-core): gate prompt_cache_key behind isOfficialOpenAIBaseUrl for third-party endpoints - #2761

Open
creatiVision wants to merge 2 commits into
MoonshotAI:mainfrom
creatiVision:fix/prompt-cache-key-clean
Open

fix(agent-core): gate prompt_cache_key behind isOfficialOpenAIBaseUrl for third-party endpoints#2761
creatiVision wants to merge 2 commits into
MoonshotAI:mainfrom
creatiVision:fix/prompt-cache-key-clean

Conversation

@creatiVision

Copy link
Copy Markdown

Problem

Since prompt_cache_key was introduced for session cache affinity, Kimi Code CLI sends this parameter to all OpenAI-compatible providers — not just to official OpenAI endpoints. Third-party APIs that strictly validate request parameters reject it with HTTP 400, making providers like NVIDIA NIM and Azure Foundry unusable.

Both issues remain open because the legacy engine (packages/agent-core) lacks the guard that agent-core-v2 already has.

Root cause

packages/agent-core/src/session/provider-manager.ts unconditionally injected prompt_cache_key into generationKwargs for every openai and openai_responses provider configuration, regardless of the actual target base URL:

// Before (openai case):
generationKwargs: { prompt_cache_key: promptCacheKey },

The agent-core-v2 engine already solved this via isOfficialOpenAIBaseUrl()prompt_cache_key is only sent when the hostname targets api.openai.com or a regional *.api.openai.com variant. Non-Kimi vendors that support the field on other hosts opt in through a cacheKey hook.

Changes

packages/agent-core/src/session/provider-manager.ts (1 file, +34 / −5):

  • Added isOfficialOpenAIBaseUrl() helper — same logic as the v2 implementation
  • openai case: gated prompt_cache_key behind isOfficialOpenAIBaseUrl
  • openai_responses case: same gating
  • kimi case: unchanged — Kimi API always accepts prompt_cache_key
  • All three cases: generationKwargs is now entirely omitted when promptCacheKey is undefined, instead of sending prompt_cache_key: undefined on the wire

Behaviour after fix

Provider config Base URL prompt_cache_key sent?
OpenAI https://api.openai.com/v1 (default) ✅ Yes
Kimi https://api.moonshot.cn/v1 ✅ Yes (direct provider)
NVIDIA NIM https://integrate.api.nvidia.com/v1 ❌ No
Azure Foundry https://{resource}.openai.azure.com/v1 ❌ No
Any custom OpenAI-compatible any other host ❌ No

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked related issues above.
  • I have added tests that prove my feature works. (Existing test suite covers this code path.)
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

Third-party OpenAI-compatible providers (NVIDIA NIM, Azure Foundry, etc.)
reject the unknown prompt_cache_key parameter with HTTP 400.

Add isOfficialOpenAIBaseUrl() guard — only send prompt_cache_key when
the base URL targets api.openai.com (or a *.api.openai.com variant),
matching the existing fix in agent-core-v2.
@changeset-bot

changeset-bot Bot commented Aug 9, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 601e041

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e430cb316b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

// `metadata.user_id` above). Only sent to official OpenAI API
// endpoints — strictly-validating OpenAI-compatible third-party
// endpoints (NVIDIA, Azure Foundry, etc.) reject unknown parameters.
...(promptCacheKey !== undefined && isOfficialOpenAIBaseUrl(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Add a changeset for this provider fix

This is a user-visible provider behavior change, but the commit only changes provider-manager.ts and adds no .changeset/ entry, so changeset-based release/version notes can omit the third-party endpoint fix. Add an appropriate changeset before this PR is submitted.

AGENTS.md reference: AGENTS.md:L84-L85

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant